package com.leetcode.LC;

import java.util.ArrayList;
import java.util.List;

public class LC1018 {
    public List<Boolean> prefixesDivBy5(int[] A) {//处理不了过长的int型数组

        ArrayList<Boolean> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        for (int i : A) {
            builder.append(i);
        }
        String s = builder.toString();
        for (int i = 1; i <= s.length(); i++) {
            list.add(ifFive(s.substring(0, i)));
        }
        return list;
    }

    //判断二进制字符串能否被5整除
    public boolean ifFive(String str) {
        if (str.equals("0")){
            return true;
        }
        StringBuilder builder = new StringBuilder(str);
        builder.reverse();
        int sum = 0;
        for (int i = 0; i < builder.length(); i++) {
            sum += (((builder.charAt(i)-'0') * (Math.pow(2, i)))%5);//就算取模虽然不会超出int范围，但会超时
        }
        return sum % 5 == 0 ? true : false;
    }

    public static void main(String[] args) {
        for (Boolean aBoolean : new LC1018().prefixesDivBy5(new int[]{1,1,1})) {
            System.out.print(aBoolean+",");
        }
    }
}
